import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import { fileURLToPath, URL } from 'node:url'

// https://vite.dev/config/
export default defineConfig({
  // build: {
  //   rollupOptions: {
  //     external: ['vue', 'vue-router', 'element-plus'],
  //     output: {
  //       globals: {
  //         vue: 'Vue',
  //         'vue-router': 'VueRouter',
  //         'element-plus': 'ElementPlus',
  //       },
  //     },
  //   },
  // },

  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes('node_modules')) {
            // 将大依赖拆分成单独 chunk
            if (id.includes('vue-element-plus-x')) {
              return 'vue-element-plus-x'
            }
            if (id.includes('d3')) {
              return 'd3'
            }
            if (id.includes('@antv/g6')) {
              return '@antv/g6'
            }

            if (id.includes('echarts')) {
              return 'echarts'
            }
            if (id.includes('md-editor-v3')) {
              return 'md-editor-v3'
            }
            if (id.includes('mermaid')) {
              return 'mermaid'
            }
            if (id.includes('highlight.js')) {
              return 'highlight.js'
            }
            if (id.includes('cropperjs')) {
              return 'cropperjs'
            }
            if (id.includes('prettier')) {
              return 'prettier'
            }
            if (id.includes('element-plus')) {
              return 'element-plus'
            }
            // 其他大依赖
            return 'vendor'
          }
        },
      },
    },
  },

  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  plugins: [
    vue(),
    AutoImport({
      imports: ['vue', 'vue-router'],
      // dirs: ['./src/api', './src/stores', './src/hooks', './libs/jax/jax-node/stores'], // 自动导入
      dts: './src/auto-imports.d.ts',
      dirs: ['./src/libs/jax/jax-node/stores', './src/ui/stores'],
    }),
  ],
  server: {
    // host: '0.0.0.0',
    // port: 1000,

    proxy: {
      '/v1': {
        // target: 'https://10.254.32.20:30446',
        // target: 'https://10.254.32.59:30445', // llz
        target: 'https://10.254.178.226', // dp
        changeOrigin: true,
        secure: false, // 忽略 HTTPS 证书验证
        // rewrite: path => path.replace(/^\/api/, '')
      },
    },
  },
})
